﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ThoiTrang.Business;
using ThoiTrang.Entities;
using System.Drawing;
using System.Xml;

public partial class detail_full : System.Web.UI.Page
{
    //ajax
    public a_Articles_ImgCollection imgs;

    public a_Article art;
    public a_Articles_Img mainImg;
    public a_CatMain catMain;
    public a_CatSub catSub;
    public a_ArticleCollection artNewr;
    public a_ArticleCollection artLaster;
    public a_Articles_ImgCollection img2s;
    public a_SEO_Content seoContent;

    public string mImg;
    public string titleImg;

    public string strContent;
    public string strFooter;
    public string strOtherPost;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["aid"] != null)
        {
            try
            {
                string pagecur = Request["cp"] != null ? Request["cp"] : "1";
                string xml = (string)Cache["detail_full_" + Request["aid"] + "_" + pagecur];
                if (xml == null)
                {
                    int total;
                    int page = Request["cp"] == null ? 1 : int.Parse(Request["cp"]);
                    art = a_ArticleService.Geta_Article(int.Parse(Request["aid"]));
                    catMain = a_CatMainService.Geta_CatMain(art.MainCatID);
                    mainImg = a_Articles_ImgService.Geta_Articles_ImgMainByAID(art.ID);
                    catSub = a_CatSubService.Geta_CatSub(art.SubCatID);
                    artNewr = a_ArticleService.Geta_ArticleListNewer(art.SubCatID, art.ID, a_ArticleColumns.ID, ThoiTrang.Data.OrderDirection.ASC, 1, 6);
                    artLaster = a_ArticleService.a_Articles_selListLaster(art.SubCatID, art.ID, a_ArticleColumns.ID, ThoiTrang.Data.OrderDirection.DESC, 1, 6);
                    seoContent = a_SEO_ContentService.Geta_SEO_ContentListByCatMainAndCatSub(art.MainCatID, art.SubCatID);

                    if (mainImg == null) return;

                    strContent = GeneralContent();
                    strFooter = GeneralFooter();
                    strOtherPost = GeneralOtherPost();
                    
                    MenuLeft1.GroupId = catMain.GroupID;
                    Page.Title = art.KTitle;

                    xml = "<CACHE>";
                    xml += "<GROUPID>" + catMain.GroupID + "</GROUPID>";
                    xml += "<TITLE>" + art.KTitle + "</TITLE>";
                    xml += "<CONTENT>" + strContent + "</CONTENT>";
                    xml += "<FOOTER>" + strFooter + "</FOOTER>";
                    xml += "<OTHERPOST>" + strOtherPost + "</OTHERPOST>";
                    xml += "</CACHE>";
                    xml = xml.Replace("<br>", "<br />");
                    Cache.Insert("detail_full_" + Request["aid"] + "_" + pagecur, xml, null, DateTime.Now.AddHours(FOConstants.ExpireCache), System.Web.Caching.Cache.NoSlidingExpiration);

                }
                else
                {
                    //XmlDocument doc = new XmlDocument();
                    //doc.LoadXml(xml);
                    Page.Title = FindTagName("TITLE", xml);//doc.GetElementsByTagName("TITLE")[0].InnerXml;//doc.GetElementById("Title").InnerXml;
                    MenuLeft1.GroupId = int.Parse(FindTagName("GROUPID", xml));
                    strContent = FindTagName("CONTENT", xml);
                    strFooter = FindTagName("FOOTER", xml);
                    strOtherPost = FindTagName("OTHERPOST", xml);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        if (Request["msg"] != null)
        {
            //msg.Visible = true;
            switch (Request["msg"])
            {
                case "1":
                    msg.InnerHtml = "<strong>Bài viết này đã được lưu!</strong> Truy cập vào <a href=user_index.aspx?t=t&amp;ckid=" + SessionManager.GetCookie() + ">Tài khoản</a> để xem mục yêu thích của bạn.";
                    break;
                default:
                    msg.Visible = false;
                    break;
            }
            strContent = strContent.Replace("</h3>", "</h3><div class=\"msg\">" + msg.InnerHtml + "</div>");
        }
    }
    public string FindTagName(string tag, string xml) //CACHE
    {
        int posStart = xml.IndexOf("<" + tag + ">") + tag.Length + 2;
        if (posStart < 0)
            return "";
        int posEnd = xml.IndexOf("</" + tag + ">");
        if (posEnd < 0)
            return "";
        return xml.Substring(posStart, posEnd - posStart);
    }
    public string GeneralContent()
    {
        string kq = @"<h3><img src=""<%=titleImg%>"" alt=""thoi trang"" height=""40"" /></h3>
					<div class=""detail-object"" <%=displayContent%>>
						<div class=""detail-des"">
							<div class=""detail-pic fl"">
								<div class=""detail-pic-img""><a href=""<%=mImg%>"" rel=""lightbox""><img src=""<%=mImg%>"" alt=""thoi trang""  /></a></div>
								<div class=""tt"">
								<%=ToolImage%>
								<p class=""anhso clear"">Ảnh số: <%=mainImg!=null?mainImg.IID:0%></p>
								</div>
							</div>
							<h5><%=art.UTitle%></h5>
							<p class=""detail-detail-content""><%=art.UBody%></p>
							<p class=""author""><%=art.UAuthor%></p>
						</div>
					</div>";
        mImg = FOConstants.Dir_ImgArticle + art.KTitle.Replace(' ', '-') + "-" + mainImg.FYear.ToString() + "-" + "d" + mainImg.AID.ToString() + mainImg.ImageNo.ToString() + ".jpg";
        if (art.LayoutFormat == FOConstants.LstStandar)
            kq = kq.Replace("<%=ToolImage%>", @"<p class=""guithiepso fl""><a href=""#"">Gửi thiệp số</a></p>
									<p class=""luualbum fl""><a href=""#"">Lưu album</a></p>
									<p class=""danhgia fl""><a href=""#"">Đánh giá</a></p>");
        else
            kq = kq.Replace("<%=ToolImage%>", "<p class=\"danhgia fl\"><a href=\"download.aspx?in=" + mImg + "\">Tải avatar</a></p>");

        titleImg = FOConstants.Dir_ImgTitle + catSub.Name.Replace(' ', '-') + "-hp.gif";
        kq = kq.Replace("<%=titleImg%>", titleImg);
        kq = kq.Replace("<%=mImg%>", mImg);
        kq = kq.Replace("<%=art.UTitle%>", art.UTitle);
        kq = kq.Replace("<%=mainImg!=null?mainImg.IID:0%>", mainImg != null ? mainImg.IID.ToString() : "0");
        kq = kq.Replace("<%=art.UAuthor%>", art.UAuthor);
        kq = kq.Replace("<%=art.UBody%>", art.UBody);
        string dp = (Request["cp"] != null && Request["cp"] != "1") ? "style=\"display:none;\"" : "";
        kq = kq.Replace("<%=displayContent%>", dp);
        return kq;
    }
    public string GeneralFooter()
    {
        string kq = "";
        string h1f, h2f;
        if (seoContent != null)
        {
            String strTemp = seoContent.H1.Replace("\"&strYearPrevious&\"", (DateTime.Now.Year - 1).ToString());
            strTemp = strTemp.Replace("\"&strYear&\"", " - " + DateTime.Now.Year.ToString());

            h1f = TTHelper.GetUpperCaseFirstLetter(art.KTitle) + " " + strTemp;

            strTemp = seoContent.H2.Replace("\"&strYearPrevious&\"", (DateTime.Now.Year - 1).ToString());
            strTemp = strTemp.Replace("\"&strYear&\"", " - " + DateTime.Now.Year.ToString());
            h2f = TTHelper.GetUpperCaseFirstLetter(strTemp) + " " + art.KTitle;
        }
        else
        {
            h1f = TTHelper.GetUpperCaseFirstLetter(art.KTitle);
            h2f = TTHelper.GetUpperCaseFirstLetter(art.KTitle);
        }
        kq += "<h1>" + h1f + "</h1>";
        kq += "\r            ";
        kq += "<h2 class=\"footer-first\">" + h2f + "</h2>";
        kq += "\r            ";
        kq += "<h4 class=\"footer-second\">" + (art.KBody != null ? TTHelper.GetUpperCaseFirstLetter(art.KBody) : "") + "</h4>";
        kq += "<br />";
        kq += "\r            ";

        if (artNewr != null && artNewr.Count > 0)
        {
            for (int i = artNewr.Count - 1; i >= 0; i--)
            {
                kq += "<a href=\"detail.aspx?aid=" + artNewr[i].ID + "&amp;ckid=" + SessionManager.GetCookie() + "\">" + artNewr[i].UTitle + "</a>";
                kq += "\r            ";
            }
        }
        if (artLaster != null && artLaster.Count > 0)
        {
            for (int i = artLaster.Count - 1; i >= 0; i--)
            {
                kq += "<a href=\"detail.aspx?aid=" + artLaster[i].ID + "&amp;ckid=" + SessionManager.GetCookie() + "\">" + artLaster[i].UTitle + "</a>";
                kq += "\r            ";
            }
        }
        return kq;
    }
    public string GeneralOtherPost()
    {
        string kq = "<div class=\"other-post-wrap\">";
        if (artNewr != null && artNewr.Count > 0)
        {
            kq += "<div class=\"other-post\"><h5>Bài mới hơn</h5><ul class=\"post\">";
            for (int i = artNewr.Count - 1; i >= 0; i--)
            {
                kq += "<li><a href=\"detail.aspx?aid=" + artNewr[i].ID + "&amp;ckid=" + SessionManager.GetCookie() + "\">" + artNewr[i].UTitle + "</a></li>";
                kq += "\r            ";
            }
            kq += "</ul></div>";
        }
        if (artLaster != null && artLaster.Count > 0)
        {
            kq += "<div class=\"other-post\" style=\"margin-bottom: 10px;\"><h5>Bài cũ hơn</h5><ul class=\"post\">";
            for (int i = 0; i < artLaster.Count; i++)
            {
                kq += "<li><a href=\"detail.aspx?aid=" + artLaster[i].ID + "&amp;ckid=" + SessionManager.GetCookie() + "\">" + artLaster[i].UTitle + "</a></li>";
                kq += "\r            ";
            }
            kq += "</ul></div>";
        }
        kq += "</div>";
        return kq;
    }
    public string GetString()
    {
        int aid = int.Parse(Request["aid"]);
        string html = "";
        a_Article aTemp = a_ArticleService.Geta_Article(aid);
        int total;
        a_Articles_ImgCollection imgs = a_Articles_ImgService.Geta_Articles_ImgListByAID(aid, a_Articles_ImgColumns.ImageW, ThoiTrang.Data.OrderDirection.ASC);
        if (imgs != null)
        {
            switch (aTemp.LayoutFormat)
            {
                case FOConstants.LstStandar:
                    html = lstImgStandar(imgs, aTemp);
                    break;
                case FOConstants.LstAvatar:
                    html = lstImgAvatar(imgs, aTemp);
                    break;
                default:
                    html = lstImgStandar(imgs, aTemp);
                    break;
            }
        }
        else
            html += "<div class=\"detail-full\"><p align=\"center\">Không có dữ liệu hiển thị!</p><div class=\"clear\"></div></div>";

        return html;
    }
    protected string lstImgStandar(a_Articles_ImgCollection imgs, a_Article aTemp)
    {
        string html = "<div class=\"detail-full\">";
        foreach (a_Articles_Img Img in imgs.ToArray())
        {
            string link = FOConstants.Dir_ImgArticle + aTemp.KTitle.Replace(' ', '-') + "-" + Img.FYear.ToString() + "-" + "d" + Img.AID.ToString() + Img.ImageNo.ToString() + ".jpg";
            int width = (Img.ImageW != null && Img.ImageW > 0) ? Img.ImageW : 250;
            string img = "<div class=\"detail-pic fl\">";
            img += "<div class=\"detail-pic-img\"><img src=\"" + link + "\" alt=\"thoi trang\" width=\"" + width.ToString() + "\" onclick=\"javascript:window.open('zoom.aspx?iid=" + link + "','child','width=510,height=560');\"  /></div>";
            img += "<div class=\"tt\">";
            img += "<p class=\"guithiepso fl\"><a href=\"#\">Gửi thiệp số</a></p>";
            img += "<p class=\"luualbum fl\"><a href=\"#\">Lưu album</a></p>";
            img += "<p class=\"danhgia fl\"><a href=\"#\">Đánh giá</a></p>";
            if (Img.Title != null && Img.Title.Length > 0)
                img += "<p class=\"price\">" + Img.Title + "</p>";
            img += "<p class=\"anhso clear\">Ảnh số: " + Img.IID.ToString() + "</p>";
            img += "</div></div>";
            html += img;
        }
        html += "<div class=\"clear\"></div></div>";
        return html;
    }
    protected string lstImgAvatar(a_Articles_ImgCollection imgs, a_Article aTemp)
    {
        string html = "<div class=\"detail-full avatar\">";
        foreach (a_Articles_Img Img in imgs.ToArray())
        {
            string link = FOConstants.Dir_ImgArticle + aTemp.KTitle.Replace(' ', '-') + "-" + Img.FYear.ToString() + "-" + "d" + Img.AID.ToString() + Img.ImageNo.ToString() + ".jpg";
            int width = (Img.ImageW != null && Img.ImageW > 0) ? Img.ImageW : 250;
            string img = "<div class=\"detail-pic fl\">";
            img += "<div class=\"detail-pic-img\"><a href=\"download.aspx?in=" + link + "\"><img src=\"" + link + "\" alt=\"thoi trang\" width=\"" + width.ToString() + "\" /></a></div>";
            img += "<div class=\"tt\">";
            img += "<p class=\"taiavatar fl\"><a href=\"download.aspx?in=" + link + "\">Tai avatar</a></p>";
            img += "<p class=\"anhso clear\">Ảnh số: " + Img.IID.ToString() + "</p>";
            img += "</div></div>";
            html += img;
        }
        html += "<div class=\"clear\"></div></div>";
        return html;
    }
}
